//
//  MSMagicSprite.m
//  MagicSword
//
//  Created by Netease on 15/11/30.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSMagicSprite.h"

@implementation MSMagicSprite {
    SKTextureAtlas *_atlas;
    NSInteger _movePageFrame;
    NSInteger _maxPageFrame;
    BOOL _needMove;
    NSString *_textureName;
    
    MSWeaponModel _type;
    float         _magicBottom;
}

- (instancetype)initWithType:(MSWeaponModel)type {
    self = [super init];
    if (self) {
        _needMove = NO;
        _type = type;
        _textureName = @"TextureImage";
        
        switch (type) {
            case MSWeaponModel_10:
                _atlas = [SKTextureAtlas atlasNamed:@"MagicA_Detail"];
                _movePageFrame = 12;
                _maxPageFrame = 23;
                _needMove = YES;
                
                break;
                
            case MSWeaponModel_11:
                _atlas = [SKTextureAtlas atlasNamed:@"MagicB_Detail"];
                _movePageFrame = 4;
                _maxPageFrame = 12;
                [self setScale:1.5];
                _magicBottom = -20;
                break;
                
            case MSWeaponModel_12:
                _atlas = [SKTextureAtlas atlasNamed:@"MagicC_Detail"];
                _movePageFrame = 7;
                _maxPageFrame = 13;
                [self setScale:1.5];
                _magicBottom = 0;
                break;
            default:
                break;
        }
    }
    return self;
}

- (void)setMagicScale {
    switch (_type) {
        case MSWeaponModel_10:
            [self setScale:3];
            break;
            
        case MSWeaponModel_11:
            [self setScale:2];
            break;
            
        case MSWeaponModel_12:
            [self setScale:2.5];
            break;
        default:
            break;
    }
}

- (void)runActionWithFristDuration:(double)fristDuration attackDuration:(double)attackDuration to:(CGPoint)toPoint block:(MSCompletionBlock)block {

    [self setMagicScale];
    
    // 1准备
    NSArray *textureArray1 = [self getTextureArray:0 end:_movePageFrame-1];
    SKAction *action1 = [SKAction animateWithTextures:textureArray1 timePerFrame:0.1];
    action1.duration = fristDuration;
    
    // 2前进
    SKTexture *texture = [_atlas textureNamed:[NSString stringWithFormat:@"%@_%d", _textureName, _movePageFrame]];
    
    SKAction *action2 = [SKAction moveTo:toPoint duration:0.2];
    if (_type == MSWeaponModel_10) {
        SKAction *action2_1 = [SKAction moveToY:70 duration:0.01];
        SKAction *action2_2 = [SKAction animateWithTextures:@[texture] timePerFrame:0.01];
        SKAction *action2_3 = [SKAction moveTo:toPoint duration:0.2];
        SKAction *action2_group = [SKAction group:@[action2_1, action2_2]];
        action2 = [SKAction sequence:@[action2_group, action2_3]];
    }
    
    // 3攻击
    NSArray *textureArray3 = [self getTextureArray:_movePageFrame-1 end:_maxPageFrame];
    SKAction *action3 = [SKAction animateWithTextures:textureArray3 timePerFrame:0.1];
    action3.duration = attackDuration;
    
    SKAction *action;
    if (_needMove) {
        action = [SKAction sequence:@[action1,action2,action3]];
    } else {
        action = [SKAction sequence:@[action1,action3]];
        self.position = CGPointMake(toPoint.x, _magicBottom);
    }
    [self runAction:action completion:block];
}

- (NSArray *)getTextureArray:(NSInteger)from end:(NSInteger)end {
    NSMutableArray *textureArray = [NSMutableArray array];
    for (NSInteger i=from; i<=end; i++) {
        SKTexture *texture = [_atlas textureNamed:[NSString stringWithFormat:@"%@_%d", _textureName, i]];
        [textureArray addObject:texture];
    }
    
    return textureArray;
}

@end
